package com.engine.modules.raster.controller;

import com.engine.common.baseEntity.jsonResponse.JsonResponseEntity;
import com.engine.common.constant.CommonConstant;
import com.engine.common.exception.InvalidTokenRuntimeException;
import com.engine.common.util.TokenVerifyUtil.AuthRedisUtil;
import com.engine.common.util.TokenVerifyUtil.TokenVerifyUtil;
import com.engine.modules.raster.mapper.RasterCommonMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "4-栅格数据通用接口")
@CrossOrigin
@RequestMapping("/Raster")
@ApiSort(value = 4)
@Slf4j
public class RasterCommonController {

    @Autowired
    RasterCommonMapper rasterCommonMapper;

    @Autowired
    AuthRedisUtil authRedisUtil;

    @ApiOperation(value = "坐标点获取栅格值", notes = "通过xy点获取栅格数据值，无统计功能速度快")
    @GetMapping(value = "/selectRasterValuesByXY/{tableName}/{token}")
    @ApiOperationSupport(order = 1)
    public String selectRasterValuesByXY(@PathVariable("tableName") String tableName,
                                         @PathVariable("token") String token,
                                         @RequestParam(value = "x") Double x,
                                         @RequestParam(value = "y") Double y,
                                         @RequestParam(value = "band") Integer band,
                                         HttpServletResponse response) {
        try {
            String access = TokenVerifyUtil.tokenVerify(token);
            List<Map<String, Object>> rsList = rasterCommonMapper.selectRasterValueByXY(tableName, x, y, band);
            if (rsList.size() > 0) {
                JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(true, CommonConstant.SC_OK_200, "查询成功", rsList.get(0), 1, response);
                return jsonResponseEntity.getJsonString();
            } else {
                JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_RESULT_NULL, "无结果", response);
                return jsonResponseEntity.getJsonString();
            }
        } catch (InvalidTokenRuntimeException e)
        {
            JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_BK_ERROR, "token错误，e：" + e.getMessage(),
                    response);
            return jsonResponseEntity.getJsonString();
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_BK_ERROR, "查询出错，e：" + e.getMessage(), response);
            return jsonResponseEntity.getJsonString();
        }
    }

    @ApiOperation(value = "geometry获取栅格值", notes = "通过Geometry（此Geometry是geojson中的数据格式），获取栅格数据平均值")
    @PostMapping(value = "/selectRasterAvgValueByGeometry/{tableName}/{token}", produces = "application/json;charset=UTF-8")
    @ApiOperationSupport(order = 2)
    @ResponseBody
    public String selectRasterAvgValueByGeometry(@PathVariable("tableName") String tableName,
                                                 @PathVariable("token") String token,
                                                 @RequestBody String geometry,
                                                 @RequestParam(value = "band") Integer band,
                                                 HttpServletResponse response) {
        try {
            String access = TokenVerifyUtil.tokenVerify(token);
            List<Map<String, Object>> rsList = rasterCommonMapper.selectRasterAvgValueByGeometry(tableName, geometry, band);
            if (rsList.size() > 0) {
                JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(true, CommonConstant.SC_OK_200, "查询成功", rsList.get(0), 1, response);
                return jsonResponseEntity.getJsonString();
            } else {
                JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_RESULT_NULL, "无结果", response);
                return jsonResponseEntity.getJsonString();
            }
        } catch (InvalidTokenRuntimeException e)
        {
            JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_BK_ERROR, "token错误，e：" + e.getMessage(),
                    response);
            return jsonResponseEntity.getJsonString();
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_BK_ERROR, "查询出错，e：" + e.getMessage(), response);
            return jsonResponseEntity.getJsonString();
        }
    }

    @ApiOperation(value = "geometry智能获取栅格值", notes = "geometry智能获取栅格值,根据临界面积，超过部分使用中心点来取值")
    @PostMapping(value = "/selectRasterAvgValueByGeometrySmart/{tableName}/{token}", produces = "application/json;charset=UTF-8")
    @ApiOperationSupport(order = 3)
    @ResponseBody
    public String selectRasterAvgValueByGeometrySmart(@PathVariable("tableNames") String tableNames,
                                                      @PathVariable("token") String token,
                                                      @RequestBody String geometry,
                                                      @RequestParam(value = "band") Integer band,
                                                      @RequestParam(value = "limitArea") Double limitArea,
                                                      HttpServletResponse response) {
        try {
            String access = TokenVerifyUtil.tokenVerify(token);
            List<Map<String, Object>> rsList = rasterCommonMapper.selectRasterAvgValueByGeometrySmart(tableNames, geometry, band, limitArea);
            if (rsList.size() > 0) {
                JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(true, CommonConstant.SC_OK_200, "查询成功", rsList.get(0), 1, response);
                return jsonResponseEntity.getJsonString();
            } else {
                JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_RESULT_NULL, "无结果", response);
                return jsonResponseEntity.getJsonString();
            }
        } catch (InvalidTokenRuntimeException e)
        {
            JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_BK_ERROR, "token错误，e：" + e.getMessage(),
                    response);
            return jsonResponseEntity.getJsonString();
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_BK_ERROR, "查询出错，e：" + e.getMessage(), response);
            return jsonResponseEntity.getJsonString();
        }
    }
}
